/**
 * @author Justin Akin
 * @version Action Pals Core Library 0.1.0
 */
package com.actionpals.components.containers
{	
	import com.actionpals.base.BaseSprite;
	import com.actionpals.enum.Align;
	import com.actionpals.graphics.RectangleShape;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	public class ScrollableContainer extends BaseSprite
	{
		
	//--------------------------------------------------------------------------------
	//
	//	Protected Member Variables
	//
	//--------------------------------------------------------------------------------
		
		/**
		 * The content to be scrolled
		 */
		protected var _display:Sprite;
		
		/**
		 * The bounds with which to calculate content scrolling.
		 */
		protected var _displayBounds:Rectangle;
		
		/**
		 * A mask of the content area.
		 */
		protected var _displayMask:RectangleShape;
		
	//--------------------------------------------------------------------------------
	//
	//	Constructor
	//
	//--------------------------------------------------------------------------------
	
		/**
		 * Constructor
		 * 
		 * @param useMask	A Boolean deciding whether the display should be masked or not.
		 */
		public function ScrollableContainer( displayBounds:Rectangle )
		{
			super();
			
			addDisplay();
			addMask();
			this.displayBounds = displayBounds;
			_display.mask = _displayMask;	
		}
		
	//--------------------------------------------------------------------------------
	//
	//	Public Methods
	//
	//--------------------------------------------------------------------------------
		
		/**
		 * Adds item to _display Sprite which holds all content to be scrolled.
		 * 
		 * @param item		The item to be added to the _display sprite.
		 */
		public function addContent( item:DisplayObject ):DisplayObject
		{	
			if( _display.numChildren > 0 ) _display.removeChildAt( 0 );
			
			return _display.addChild( item );
		}
		
		
		/**
		 * Scrolls _display based on percentage.
		 * 
		 * @param percent		The percent to be scrolled.  0 = Top : 1 = Bottom.
		 * @param orientation	Represents from which direction to calculate the scroll.
		 */
		public function scrollByPercent( percent:Number = 0, align:String = Align.LEFT ):void
		{	
			var bRect:Rectangle = _displayBounds;
			var cRect:Rectangle = _display.getBounds( this );
			var total:Number;
			
			switch( align )
			{	
				case Align.LEFT :
					
					total = bRect.width - cRect.width;
					_display.x = total * percent;
					break;
					
				case Align.RIGHT :
				
					total = bRect.width - cRect.width;
					_display.x = total - ( total * percent );
					break;
					
				case Align.TOP :
				
					total = bRect.height - cRect.height;
					_display.y = total * percent;
					break;
					
				case Align.BOTTOM :
				
					total = bRect.width - cRect.width;
					_display.y = total - ( total * percent );
					break;
			}			
		}
		
		/**
		 * Scrolls _display based on a number of pixels.
		 * 
		 * @param pixels		The number of pixels to be scrolled.
		 * @param align	Represents from which direction to calculate the scroll.
		 */
		public function scrollByPixels( pixels:int, align:String = Align.LEFT ):void
		{	
			var bRect:Rectangle = _displayBounds;
			
			switch( align )
			{	
				case Align.LEFT :
					
					_display.x = bRect.x + pixels;
					break;
					
				case Align.RIGHT :
				
					_display.x = bRect.right - pixels;
					break;
					
				case Align.TOP :
				
					_display.y = bRect.y + pixels;
					break;
					
				case Align.BOTTOM :
				
					_display.y = bRect.bottom - pixels;
					break;
			}
		}
		
	//--------------------------------------------------------------------------------
	//
	//	Getters And Setters
	//
	//--------------------------------------------------------------------------------
		
		/**
		 * Sets the width and height of _displayBounds.
		 * 
		 * @param w		The new width of _displayBounds.
		 * @param h		The new height of _displayBounds.
		 */		
		public function set displayBounds( displayBounds:Rectangle ):void
		{				
			_displayBounds = displayBounds;
			resizeMask();			
		}
		
		public function get displayBounds():Rectangle
		{
			return _displayBounds;
		}
		
		public function get displayWidth():Number
		{
			return _display.width;
		}
		
		public function get displayHeight():Number
		{
			return _display.height;
		}
		
		public function get widthRatio():Number
		{
			return _displayBounds.width / displayWidth;
		}
		
		public function get heightRatio():Number
		{
			return _displayBounds.height / displayHeight;
		}
		
		public function get widthDiff():Number
		{
			return displayWidth - _displayBounds.width;
		}
		
		public function get heightDiff():Number
		{
			return displayHeight - _displayBounds.height;
		}
		
		public function get xDiff():Number
		{
			return Math.abs( _display.x );
		}
		
		public function get yDiff():Number
		{
			return Math.abs( _display.y );
		}
		
		public function get xPercent():Number
		{
			return xDiff / widthDiff;
		}
		
		public function get yPercent():Number
		{
			return yDiff / heightDiff;
		}
		
		public function get contentRatio():Number
		{
			return _displayBounds.height / displayHeight;
		}
		
		public function get contentHRatio():Number
		{
			return _displayBounds.width / displayWidth;
		}
		
	//--------------------------------------------------------------------------------
	//
	//	Protected Functions
	//
	//--------------------------------------------------------------------------------
		
		/**
		 * 
		 */
		protected function addMask():void
		{	
			_displayMask = addChild( new RectangleShape( 10, 10, 0, 0 ) ) as RectangleShape;
		}
		
		/**
		 * 
		 */
		protected function addDisplay():void
		{			
			_display = addChild( new Sprite() ) as Sprite;		
		}
		
		/**
		 * 
		 */
		protected function resizeMask():void
		{	
			_displayMask.width = _displayBounds.width;
			_displayMask.height = _displayBounds.height;	
		}		
	}	
}